package com.example.mybmicalculator;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.mybmicalculator.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;
    int chooseButton = 0;
    double BMI;
    String text = "";


    //表示当activity创建选项菜单时回调的方法
    @Override
    public boolean onCreateOptionsMenu(Menu menu1){
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.meun1,menu1);
        menu1.add(Menu.NONE,1,3,"帮助").setIcon(R.drawable.ic_baseline_help_24);


        return true;
    }

    @Override
    public  boolean onOptionsItemSelected(MenuItem item){
        //处理菜单项的选择
        switch (item.getItemId()){
            case R.id.item_qingkong:
                Toast.makeText(MainActivity.this,"清空",Toast.LENGTH_LONG).show();
                binding.etWeight.setText("");
                binding.etHeight.setText("");
                binding.rbtnMan.setChecked(false);
                binding.rbtnWoman.setChecked(false);
                binding.tvBMIJG.setText("");
                binding.tvDiagnosisJG.setText("");
                return true;
            case R.id.item_settings:
                Toast.makeText(MainActivity.this,"设置",Toast.LENGTH_LONG).show();
                return true;
            case Menu.FIRST:
                Toast.makeText(MainActivity.this,"帮助",Toast.LENGTH_LONG).show();
                return true;
            default:
                return super.onOptionsItemSelected(item);

        }
    }





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initData();

    }

    private void initData() {




        binding.rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i == R.id.rbtn_man) {
                    chooseButton = 1;
                } else {
                    chooseButton = 2;
                }
            }
        });


        binding.btnCalculate.setOnClickListener(view -> {


            if (TextUtils.isEmpty(binding.etWeight.getText().toString().trim())
                    || TextUtils.isEmpty(binding.etHeight.getText().toString().trim())
                    || chooseButton == 0) {

                Toast.makeText(this, "请输入数值", Toast.LENGTH_SHORT).show();

            } else {
                String tz = binding.etWeight.getText().toString().trim();
                String sz = binding.etHeight.getText().toString().trim();

                BMI = Math.round(Double.parseDouble(tz) / Math.pow(Double.parseDouble(sz), 2));
                if (chooseButton == 1) {
                    if (BMI < 20) {
                        text = "体重过轻";
                    } else if (BMI < 25) {
                        text = "体重正常";
                    } else if (BMI < 27) {
                        text = "体重超重";
                    } else if (BMI < 30) {
                        text = "轻度肥胖";
                    } else if (BMI < 35) {
                        text = "中度肥胖";
                    } else if (BMI > 35) {
                        text = "重度肥胖";
                    }

                    binding.tvBMIJG.setText(BMI + "");
                    binding.tvDiagnosisJG.setText(text);
                }
                if (chooseButton == 2) {
                    if (BMI < 19) {
                        text = "体重过轻";
                    } else if (BMI < 24) {
                        text = "体重正常";
                    } else if (BMI < 26) {
                        text = "体重超重";
                    } else if (BMI < 29) {
                        text = "轻度肥胖";
                    } else if (BMI < 34) {
                        text = "中度肥胖";
                    } else if (BMI > 34) {
                        text = "重度肥胖";
                    }

                    binding.tvBMIJG.setText(BMI + "");
                    binding.tvDiagnosisJG.setText(text);
                }
            }
        });




    }


}
